package ma.cdm.rapports.datasources;

import java.util.List;

import ma.cdm.compteRenduVO.DetailPointVO;
import ma.cdm.compteRenduVO.EntiteVO;
import ma.cdm.compteRenduVO.PersonneVO;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class PointDataSource implements JRDataSource {
    private List<DetailPointVO> points;
    private int index = -1;
    public PointDataSource(List<DetailPointVO> points) {
        super();
        this.points = points;
    }
    public Object getFieldValue(JRField field) throws JRException {
        String fieldName = field.getName();
        DetailPointVO pt = points.get(index);
        if ("pointDiscussion".equals(fieldName)) {
            return pt.getSuiviPoint().getPoint().getTitre();
        }else if ("presentePar".equals(fieldName)) {
            return pt.getSuiviPoint().getPresentePar();
        }else if ("decision".equals(fieldName)) {
            return pt.getDecision();
        } 
        else if ("status".equals(fieldName)) {
        	if(pt.getStatus() == null)
        	{
        		return "";
        	}
            return pt.getStatus();
        }
        else if ("action".equals(fieldName)) {
        	if(pt.getAction() == null)
        	{
        		return "";
        	}
            return pt.getAction();
        } 
        
        else if ("responsables".equals(fieldName)) {
        	String responsablesStr="";
        	if(pt.getEntitesResponsables() != null)
        	{
        		for (int i = 0; i < pt.getEntitesResponsables().size(); i++) {
        			EntiteVO responsable = pt.getEntitesResponsables().get(i);
        			responsablesStr=responsablesStr+"-"+responsable.getNom(); 
        			if(i < pt.getEntitesResponsables().size()-1)
            		{
            			responsablesStr+="\n";
            		}
        		}
        	}
        	
        	if(!responsablesStr.equals(""))
        	{
        		if(pt.getResponsables() != null && pt.getResponsables().size() >0)
        		{
        			responsablesStr+="\n";
        		}
        	}
        	if(pt.getResponsables() != null)
        	{        		
        		for (int i = 0; i < pt.getResponsables().size(); i++) {
            		PersonneVO responsable = pt.getResponsables().get(i);
            		responsablesStr=responsablesStr+"-"+responsable;
            		if(i < pt.getResponsables().size()-1)
            		{
            			responsablesStr+="\n";
            		}
    			}
        	}
        	
        	
        	
            return responsablesStr;
        } 
        else if ("delai".equals(fieldName)) {
        	if(pt.getDateRealisation() == null)
        	{
        		return "";
        	}
            return pt.getDateRealisation();
        }
        else if ("numPoint".equals(fieldName)) {
            return pt.getSuiviPoint().getPoint().getNumPoint()+1;
        } 
        else if ("numDetail".equals(fieldName)) {
            return (pt.getSuiviPoint().getPoint().getNumPoint()+1)+"."+(pt.getNumDetail()+1);
        } 
        else if ("hideHeader".equals(fieldName)) {
            return (pt != null && pt.getSuiviPoint()!= null && pt.getSuiviPoint().getDetailsPoint().size() <= 1);
        } 
       
        return "";
    }
    public boolean next() throws JRException {
        return ++index < points.size();
    }
}